﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using StackExchange.Redis;
namespace Infra_common.redis
{
    public class RedisEvent
    {
        private IConnectionMultiplexer _redis;

        public RedisEvent(IConnectionMultiplexer multiplexer)
        {
            _redis = multiplexer;
        }
        /// <summary>
        /// 启动事件监听
        /// </summary>
        /// <param name="channel"></param>
        /// <returns></returns>
        public async Task GetSub(string channelkey)
        {
            ISubscriber subscriber = _redis.GetSubscriber();
            await subscriber.SubscribeAsync(channelkey, (channel, message) =>
            {
                GetMessage(message, channel);
            });
        }

        /// <summary>
        /// 对外返回事件监听结果
        /// </summary>
        /// <param name="message"></param>
        /// <param name="channel"></param>
        /// <returns></returns>
        public Tuple<string,string> GetMessage(string message,string channel)
        {
            return new Tuple<string, string>(channel, message);
        }
    }
}
